package services;

import java.util.Collection;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import repositories.AssessRepository;
import domain.Assess;

@Service
@Transactional
public class AssessService {
	
	//Managed repository
	@Autowired
	private AssessRepository assessRepository;

	//Supporting services
	@Autowired
	private ParticipantService participantService;
	
	//Constructors
	public AssessService() {
		super();
	}
	//CRUD methods
	public Assess create() {
		Assess assess = new Assess();
		assess.setParticipant(participantService.findByPrincipal());
		return assess;
	}
	
	public Collection<Assess> findAll(){
		Collection<Assess> result;
		result = assessRepository.findAll();
		return result;
	}
	
	
	public Assess findOne(int assessId){
		Assess result;
		result = assessRepository.findOne(assessId);
		return result;
	}
	

	public void save(Assess assess) {
		Date currentMoment = new Date();
		Assert.notNull(assess);
		Assert.isTrue(assess.getParticipant().equals(
				participantService.findByPrincipal()));
		Assert.isTrue(assess.getRoute().getEvent().getFinishMoment().compareTo(currentMoment)<1);

		assessRepository.save(assess);
	}
	

	public void delete(Assess assess){
		Assert.notNull(assess);
		Assert.isTrue(assess.getParticipant().equals(
				participantService.findByPrincipal()));
		assessRepository.delete(assess);
	}
	
	//Other bussiness methods
	
	public Collection<Assess> findByParticipantId(int id){
		Collection<Assess> result;
		result = assessRepository.findByParicipantId(id);
		return result;
	}
	

}
